//
//  SignView.swift
//  mysigndemo
//
//  Created by dengwei on 16/6/2.
//  Copyright © 2016年 dengwei. All rights reserved.
//
import UIKit

enum SignViewButtonType: Int {
    case WEIXIN = 1
    case SHARE = 2
}

class SignView: UIView{
    
    var signViewClick:((type: SignViewButtonType) -> ())?
    weak var signVC: UIViewController?
    let backImageView        = UIImageView()
    let closeButton       = UIButton()
    var iconImageView: HYBLoadingImageView!
    let titleLabel = UILabel()
    let weixinButton = UIButton()
    let shareButton = UIButton()
    
    let signLabel = UILabel()
    
    let signPaimingLabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        UIView.animateWithDuration(0.4, animations: { () -> Void in
            self.frame = frame
        })
        
        //backImageView
        backImageView.image = UIImage(named: "sign_icon_popbg")
        //backImageView.contentMode = UIViewContentMode.ScaleAspectFit
        //backImageView.contentMode = UIViewContentMode.ScaleAspectFill
        backImageView.contentMode = UIViewContentMode.Top
        addSubview(backImageView)
        
        iconImageView = HYBLoadingImageView()
        iconImageView.image = UIImage(named:"ic_login_qq_nor")
        iconImageView.isCircle(true)
        addSubview(iconImageView)
        
        //signLabel
        signLabel.text = "签到成功 积分 + 5"
        signLabel.numberOfLines = 1
        signLabel.textColor = LFBGlobalSignSuccessTextColor
        signLabel.font = UIFont.systemFontOfSize(18)
        signLabel.textAlignment = .Center
        addSubview(signLabel)
        
        //signPaimingLabel
        signPaimingLabel.text = "周榜排名100名"
        signPaimingLabel.numberOfLines = 1
        signPaimingLabel.textColor = LFBGlobalSignPaimingTextColor
        signPaimingLabel.font = UIFont.systemFontOfSize(14)
        signPaimingLabel.textAlignment = .Center
        addSubview(signPaimingLabel)
        
        //closeButton
        closeButton.setBackgroundImage(UIImage(named:"sign_icon_close"), forState: .Normal)
        closeButton.setBackgroundImage(UIImage(named:"sign_icon_close"), forState: .Highlighted)
        addSubview(closeButton)
        closeButton.addTarget(self, action: "coverClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //regButton
        weixinButton.setTitleColor(LFBTextWhiteColor, forState: .Normal)
        weixinButton.setBackgroundImage(UIImage.imageWithColor(LFBGlobalButtonBackNormalColor,size:CGSizeMake(90, 30),alpha: 1),forState: .Normal)
        weixinButton.setBackgroundImage(UIImage.imageWithColor(LFBGlobalButtonBackHighedColor,size:CGSizeMake(90, 30),alpha: 1),forState: UIControlState.Highlighted)
        weixinButton.setTitle(" 微信晒周榜 ", forState: .Normal)
        weixinButton.layer.masksToBounds = true
        weixinButton.layer.cornerRadius = 5
        weixinButton.titleLabel?.font = UIFont.systemFontOfSize(14)
        addSubview(weixinButton)
        //forgetButton
        shareButton.setTitleColor(LFBTextWhiteColor, forState: .Normal)
        shareButton.setTitle(" 分享给好友 ", forState: .Normal)
        shareButton.titleLabel?.font = UIFont.systemFontOfSize(14)
        shareButton.layer.masksToBounds = true
        shareButton.layer.cornerRadius = 5

        shareButton.setBackgroundImage(UIImage.imageWithColor(LFBGlobalButtonBackNormalColor,size:CGSizeMake(80, 35),alpha: 1),forState: .Normal)
        shareButton.setBackgroundImage(UIImage.imageWithColor(LFBGlobalButtonBackHighedColor,size:CGSizeMake(80, 35),alpha: 1),forState: UIControlState.Highlighted)
        addSubview(shareButton)
        //button event
        weixinButton.tag = 1
        shareButton.tag = 2
        
        for index in 1...5 {
            //print("index=\(index)")
            let tap = UITapGestureRecognizer(target: self, action: "click:")
            let subView = viewWithTag(index)
            subView?.addGestureRecognizer(tap)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func coverClick()  {
        hideSignView()
    }
    
    func hideSignView() {
        coverBtn.removeFromSuperview()
        UIView.animateWithDuration(0.4, animations: { () -> Void in
            self.frame = CGRectMake(0, AppHeight, AppWidth, 285)
            }) { (finsch) -> Void in
                self.removeFromSuperview()
        }
    }
    
    
    func click(tap: UIGestureRecognizer) {
        
        if signViewClick != nil {
            hideSignView()
            
            //print("tap.view!.tag=\(tap.view!.tag)")
            switch tap.view!.tag {
                
            case SignViewButtonType.WEIXIN.rawValue:
                //print("weixin")
                signViewClick!(type: SignViewButtonType.WEIXIN)
                break
                
            case SignViewButtonType.SHARE.rawValue:
                // print("share")
                signViewClick!(type: SignViewButtonType.SHARE)
                break
                
            default: break
            }
        }
    }
    
    private lazy var coverBtn: UIButton! = {
        //print("coverBtn")
        let coverBtn = UIButton(frame: MainBounds)
        coverBtn.backgroundColor = UIColor.blackColor()
        coverBtn.alpha = 0.2
        coverBtn.addTarget(self, action: "coverClick", forControlEvents: UIControlEvents.TouchUpInside)
        return coverBtn
    }()
    
    func showSignView(rect: CGRect) {
        self.superview?.insertSubview(coverBtn, belowSubview: self)
        UIView.animateWithDuration(0.4, animations: { () -> Void in
            self.frame = rect
        })
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        backImageView.frame = CGRectMake(0, 0, AppWidth - 20, 285)
        iconImageView.frame = CGRectMake((AppWidth - 10) * 0.5 - 40, CGRectGetMinY(backImageView.frame) + 5, 70, 70)
        closeButton.frame = CGRectMake(AppWidth - 80, 20, 25, 25)
        signLabel.frame = CGRectMake( (AppWidth - 10) * 0.5 - 100, 110, 200, 25)
        signPaimingLabel.frame = CGRectMake( (AppWidth - 10) * 0.5 - 100, CGRectGetMaxY(signLabel.frame) + 20, 200, 25)
        weixinButton.frame = CGRectMake(40,CGRectGetMaxY(signPaimingLabel.frame) + 15,90,30)
        shareButton.frame = CGRectMake(AppWidth - 150, CGRectGetMaxY(signPaimingLabel.frame) + 15,90,30)
    }
}
